package com.ado.bazinga.core.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ado.bazinga.core.annotation.ApiDesc;
import com.ado.bazinga.core.annotation.UserLoginToken;
import com.ado.bazinga.core.common.http.HttpResult;
import com.ado.bazinga.core.common.params.dto.ShopCartDto;
import com.ado.bazinga.core.common.params.vo.GoodVO;
import com.ado.bazinga.core.common.params.vo.ShopCartVO;
import com.ado.bazinga.core.common.params.vo.UserParam;
import com.ado.bazinga.core.dal.entity.Good;
import com.ado.bazinga.core.dal.entity.ShopCart;
import com.ado.bazinga.core.dal.entity.User;
import com.ado.bazinga.core.exception.BazingaException;
import com.ado.bazinga.core.service.GoodService;
import com.ado.bazinga.core.service.ShopCartService;
import com.ado.bazinga.core.service.UserService;

/**
 *@author adonais
 *@date 2020年3月26日
 *@description ShopCartController.java 购物车控制器
**/
@RestController
@RequestMapping(value = "/api/shopCart")
public class ShopCartController {
	
	@Autowired
	@Qualifier(value = "shopCartService")
	private ShopCartService shopCartService ;
	@Autowired
	@Qualifier(value = "goodService")
	private GoodService goodService ;
	@Autowired
	@Qualifier(value = "userService")
	private UserService userService ;
	
	@ApiDesc(desc = "添加商品到购物车接口")
	@UserLoginToken
	@PostMapping(value = "/createShopCart")
	public HttpResult createShopCart(@RequestBody ShopCartDto shopCartDto) {
		if (StringUtils.isEmpty(shopCartDto.getUserId().toString())) {
			throw new BazingaException("用户id不能为空");
		}
		if (StringUtils.isEmpty(shopCartDto.getGoodsId().toString())) {
			throw new BazingaException("商品id不能为空");
		}
		if (shopCartDto.getAmount() <= 0) {
			throw new BazingaException("商品数量不能小于或等于0") ;
		}
		ShopCart shopCart = new ShopCart() ;
		shopCart = assembleDtoToModel(shopCart, shopCartDto);
		boolean flag = shopCartService.createShopCart(shopCart);
		if (flag) {
			return HttpResult.ok("商品到购物车成功");
		}
		return HttpResult.error("商品添加到购物车失败") ;
	}
	
	@ApiDesc(desc = "查看用户购物车接口")
	@UserLoginToken
	@GetMapping(value = "/getUserShopCarts")
	public HttpResult getUserShopCarts(String userId) {
		if (StringUtils.isEmpty(userId)) {
			throw new BazingaException("用户id不能为空");
		}
		//TODO 组装参数应把信息补全
		List<ShopCart> shopCarts = shopCartService.getUserShopCarts(Integer.valueOf(userId));
		if (shopCarts.size() <= 0) {
			return HttpResult.error(421, "购物车暂无商品") ;
		}
		return HttpResult.ok(shopCarts);
	}
	
	@ApiDesc(desc = "删除购物车的一条记录接口")
	@UserLoginToken
	@PostMapping(value = "/deleteShopCart")
	public HttpResult deleteShopCart(String shopCartId) {
		if (StringUtils.isEmpty(shopCartId)) {
			throw new BazingaException("购物车记录id不能为空");
		}
		boolean flag = shopCartService.deleteShopCart(Integer.valueOf(shopCartId));
		if (flag) {
			return HttpResult.ok("删除购物车商品成功");
		}
		return HttpResult.error("删除购物车商品失败") ;
	}
	
	@ApiDesc(desc = "按id查询购物车记录")
	@UserLoginToken
	@GetMapping(value = "/getShopCartById")
	public HttpResult getShopCartById(String shopCartId) {
		if(StringUtils.isEmpty(shopCartId)) {
			throw new BazingaException("id不能为空");
		}
		ShopCart shopCart = shopCartService.getShopCartById(shopCartId) ;
		ShopCartVO shopCartVo = new ShopCartVO() ;
		if(shopCart == null) {
			return HttpResult.error(421, "暂未找到记录");
		}
		shopCartVo.setAmount(shopCart.getAmount().toString());
		Good good = goodService.getGoodById(shopCart.getGoodsId()) ;
		GoodVO goodVO = new GoodVO() ;
		goodVO.setName(good.getName());
		shopCartVo.setGood(goodVO);
		User user = userService.findUserById(shopCart.getUserId()) ;
		UserParam userParam = new UserParam() ;
		userParam.setUsername(user.getUsername());
		shopCartVo.setUser(userParam);
		return HttpResult.ok(shopCartVo) ;
	}
	
	/***
	 * dto转实体
	 * @param shopCart
	 * @param shopCartDto
	 * @return
	 */
	public ShopCart assembleDtoToModel(ShopCart shopCart , ShopCartDto shopCartDto) {
		try {
			BeanUtils.copyProperties(shopCart, shopCartDto);
		} catch (IllegalAccessException | InvocationTargetException e) {
			throw new BazingaException("shopcart dto assemble model failed");
		}
		return shopCart ;
	}
	

}
